﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFromTools
{
    public partial class ParseTextForm : Form
    {
        public ParseTextForm()
        {
            InitializeComponent();
        }

        private void btnSelectOriginal_Click(object sender, EventArgs e)
        {
            //if (folderDialog.ShowDialog() == DialogResult.OK)
            //{
            //    txtOriginalPath.Text = folderDialog.SelectedPath;
            //}

            if(fileDialog.ShowDialog() == DialogResult.OK)
            {
                txtOriginalPath.Text = fileDialog.FileName;
            }
        }

        private void btnParseTextFiles_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtOriginalPath.Text) || txtOriginalPath.Text.Length < 4)
            {
                MessageBox.Show("请先选择Text文件");
            }
            else
            {
                //ParseXSDFiles(txtOriginalPath.Text, txtFileNames, txtOriginalPath.Text);
                //System.IO.File.WriteAllText(filename.ToLower().Replace(".xsd", ".html"), html);
                var sql = ParseTextFile(txtOriginalPath.Text);

                WriteLine(txtFileNames, sql);

                MessageBox.Show("处理完毕!");
            }
        }

        private static void WriteLine(TextBox txtBox, String text)
        {
            txtBox.Focus();
            txtBox.AppendText(text + Environment.NewLine);
            txtBox.ScrollToCaret();
            System.Windows.Forms.Application.DoEvents();
        }

        private static string ParseTextFile(string filename)
        {
            var lines = System.IO.File.ReadAllLines(filename);
            var sb = new StringBuilder("");
            //string previous = null;
            foreach (var line in lines)
            {
                var o = line.Trim().Split('\t');
                if(o.Length > 1)
                {
                    //var parentCode = GetParentCode(o[0]);
                    // sb.Append("SELECT '").Append(o[0]).Append("', '").Append(o[1]).Append("' UNION").Append(Environment.NewLine);
                    //sb.Append("SELECT '")
                    //   .Append(o[0]).Append("', '")
                    //   .Append(o[1]).Append("', '")
                    //   .Append(o[1]).Append("', '', 1, 1, '")
                    //   .Append(parentCode)
                    //   .Append("' UNION").Append(Environment.NewLine);

                    // 
                    //var parentCode = o[0];
                    //if (o[1].EndsWith("0"))
                    //{
                    //    // 13	1311	高级兽医师	兽医师	助理兽医师	兽医技术员

                    //    for (var i = 2; i < o.Length; i++)
                    //    {
                    //        if (String.IsNullOrWhiteSpace(o[i]))
                    //            continue;

                    //        sb.Append("SELECT '")
                    //       .Append(o[0] + o[1].Substring(2, 1) + (i - 1).ToString() ).Append("', '")
                    //       .Append(o[i]).Append("', '")
                    //       .Append(parentCode).Append("',  1, 1 UNION")
                    //       .Append(Environment.NewLine);
                    //    }
                    //}
                    //else
                    //{
                    //    // 生成父级记录
                    //    sb.Append("SELECT '")
                    //       .Append(o[0]).Append("', '")
                    //       .Append(o[1]).Append("', NULL,  1, 1 UNION")
                    //       .Append(Environment.NewLine);

                    //    for(var i = 2; i < o.Length; i++)
                    //    {
                    //        if (String.IsNullOrWhiteSpace(o[i]))
                    //            continue;
                    //        sb.Append("SELECT '")
                    //       .Append(o[0] + '0' + (i - 1).ToString()).Append("', '")
                    //       .Append(o[i]).Append("', '")
                    //       .Append(parentCode).Append("',  1, 1 UNION")
                    //       .Append(Environment.NewLine);
                    //    }
                    //}


                    // <LocaleResource Name="Account.Fields.Name"><Value>姓名</Value></LocaleResource>

                    sb.Append("<LocaleResource Name=\"Country.")
                       .Append(o[1]).Append("\"><Value>")
                       .Append(o[2]).Append("</Value></LocaleResource>")
                       .Append(Environment.NewLine);

                    // INSERT INTO base_university (`Code`, `Name`, `Abbreviation`, `Lookup`, `DisplayOrder`, `ProvinceCode`, 
                    // `BuildType`, `BuildTypeName`, `SchoolType`, `SchoolTypeName`) 
                    // 
                    //sb.Append("SELECT '")
                    //    .Append(o[0]).Append("', '")
                    //    .Append(o[1]).Append("', '', '', 1, '")
                    //    .Append(o[2] + "0000").Append("', '")

                    //    .Append(o[3]).Append("', '")
                    //    .Append(o[4]).Append("', '")
                    //    .Append(o[5]).Append("', '")
                    //    .Append(o[6]).Append("', '")

                    //    .Append("' UNION").Append(Environment.NewLine);

                    //previous = line.Trim();



                }

            }
            //System.IO.File.WriteAllText(filename.ToLower().Replace(".xsd", ".html"), html);

            //return sb.ToString();

            var r = sb.ToString();

            return r;
        }

        private static string GetParentCode( string self)
        {
            ////if (String.IsNullOrEmpty(previous))
            ////    return null;

            //// 处理职业 
            //if (self.EndsWith("000000"))
            //{
            //    return null;
            //}

            //if (self.EndsWith("0000"))
            //{
            //    return self.Substring(0, self.Length - 6) + "000000";
            //    //return previous;
            //}
            //if (self.EndsWith("00"))
            //{
            //    return self.Substring(0, self.Length - 4) + "0000";
            //}
            //if (!self.EndsWith("00"))
            //{
            //    return self.Substring(0, self.Length - 2) + "00";
            //}
            //return null;
            if (self.Length == 2)
            {
                return "NULL";
            }
            if (self.Length == 4)
            {
                return self.Substring(0, 2);
            }

            if (self.Length > 4)
            {
                return self.Substring(0, 4);
            }
            return "NULL";
        }
    }
}
